import * as Cesium from 'cesium'


export class PolylineTrailLinkMaterialProperty {
  constructor(opt) {
    this._definitionChanged = new Cesium.Event()
    this._color = undefined
    this._colorSubscription = undefined
    this.color = opt.color
    this.duration = opt.duration
    this._time = new Date().getTime()
}
// 材质类型
getType(time) {
   return 'PolylineTrailLink'
}
// 这个方法在每次渲染时被调用，result的参数会传入glsl中。
getValue(time, result) {
  if (!Cesium.defined(result)) {
    result = {}
  }
  result.color = Cesium.Property.getValueOrClonedDefault(this._color, time, Cesium.Color.WHITE, result.color)
  result.image = Cesium.Material.PolylineTrailLinkImage
  result.time = ((new Date().getTime() - this._time) % this.duration) / this.duration
  return result
}
equals(other) {
  return this === other || (other instanceof PolylineTrailLinkMaterialProperty && Cesium.Property.equals(this._color, other._color))
}
}
   
  Object.defineProperties(PolylineTrailLinkMaterialProperty.prototype, {
    isConstant: {
      get: function () {
        return false
      }
    },
    definitionChanged: {
      get: function () {
        return this._definitionChanged
      }
    },
    color: Cesium.createPropertyDescriptor('color')
  })
   
  Cesium.Material._materialCache.addMaterial('PolylineTrailLink', {
    fabric: {
      type: 'PolylineTrailLink',
      uniforms: {
        color: new Cesium.Color(0.0, 1.0, 0.0, 1), // light color
        speed: 10, // flowing speed, speed > 0.0
        headsize: 0.05, // 0.0 < headsize < 1.0
        tailsize: 0.5, // 0.0 < tailsize < 1.0
        widthoffset: 0.1, // 0.0 < widthoffset < 1.0
        coresize: 0.05, // 0.0 < coresize < 1.0
      },
      source:   "float SPEED_STEP = 0.01; \n" +
      "vec4 drawLight(float xPos, vec2 st, float headOffset, float tailOffset, float widthOffset){ \n" +
      "float lineLength = smoothstep(xPos + headOffset, xPos, st.x) - smoothstep(xPos, xPos - tailOffset, st.x); \n" +
      "float lineWidth = smoothstep(widthOffset, 0.5, st.y) - smoothstep(0.5, 1.0 - widthOffset, st.y); \n" +
      "return vec4(lineLength * lineWidth); \n" +
      "}\n" +
      "czm_material czm_getMaterial(czm_materialInput materialInput) \n" +
      "{ \n" +
      "czm_material m = czm_getDefaultMaterial(materialInput);\n" +
      "float sinTime = sin(czm_frameNumber * SPEED_STEP * speed); \n" +
      "vec4 v4_core;\n" +
      "vec4 v4_color;\n" +
      "float xPos = 0.0; \n" +
      "if (sinTime < 0.0){ \n" +
      "xPos = cos(czm_frameNumber * SPEED_STEP * speed)+ 1.0 - tailsize; \n" +
      "}else{ \n" +
      "xPos = -cos(czm_frameNumber * SPEED_STEP * speed)+ 1.0 - tailsize; \n" +
      "} \n" +
      "v4_color = drawLight(xPos, materialInput.st, headsize, tailsize, widthoffset);\n" +
      "v4_core = drawLight(xPos, materialInput.st, coresize, coresize*2.0, widthoffset*2.0);\n" +
      "m.diffuse = color.xyz + v4_core.xyz*v4_core.w*0.8; \n" +
      "m.alpha = pow(v4_color.w, 3.0); \n" +
      "return m; \n" +
      "} \n"
    },
    translucent: function (material) {
      return true
    }
  })